<template>
	<el-dialog :title="title" :visible.sync="visible" width="768px">
		<div class="sign_board_wrapper" v-loading="loading">
			<h3><slot name="body"></slot></h3>
			<template v-if="!isHasSign">
				<vue-sign ref="esign" :width="738" :height="400" :line-width="3"></vue-sign>
			</template>
			<img :src="initValue" v-else style="display: block; width: 738px; height: 400px" />
			<div class="time">签名时间：{{ signTime }}</div>
		</div>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" plain @click="handleClearSign()">清空签字</el-button>
			<el-button size="medium" type="primary" @click="handleSubmitSign()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
import moment from 'moment';
import vueSign from 'vue-esign';
import { attachUpload } from '@/api/global';
import { getServerTime } from '@/api/openApi';
let timer = null;
export default {
	components: { vueSign },
	props: {
		value: { type: Boolean, default: false },
		title: { type: String, default: '' },
		initValue: { type: String, default: '' }
	},
	data() {
		return {
			visible: this.value,
			loading: false,
			nowTime: '',
			isHasSign: false
		};
	},
	computed: {
		signTime() {
			return moment(this.nowTime).format('YYYY-MM-DD HH:mm:ss');
		},
		isSign() {
			return !!this.initValue;
		}
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		async visible(flag) {
			this.$emit('input', flag);
			if (!flag) {
				clearInterval(timer);
			} else {
				this.loading = true;
				await this.getServerTimeData();
				timer = setInterval(() => {
					let curTime = moment.utc(this.nowTime).add(1, 's');
					this.nowTime = curTime.format('YYYY-MM-DD HH:mm:ss');
				}, 1000);
				this.loading = false;
			}
		},
		isSign(flag) {
			this.isHasSign = flag;
		}
	},
	methods: {
		//获取服务器时间
		async getServerTimeData() {
			let result = await getServerTime().then((res) => res.data);
			if (result.code === '0') {
				this.nowTime = this.$detailData(result.data);
			}
		},

		//重新签字
		handleClearSign() {
			if (!this.isSign) {
				this.$refs['esign'].reset();
			} else {
				this.isHasSign = false;
				this.$nextTick(() => {
					this.$refs['esign'].reset();
				});
			}
		},

		//提交签字
		async handleSubmitSign() {
			if (!this.isHasSign) {
				try {
					let res = await this.$refs['esign'].generate();
					let date = moment(this.signTime).format('YYYYMMDDHHmmss');
					let file = this.$dataURLtoFile(res, `SIGN${date}.png`);
					this.signUploadRequest(file);
				} catch {
					this.$showCustomMessage('warning', '请先签字在提交');
				}
			} else {
				this.$emit('finish', { originalAddr: this.initValue });
			}
		},

		//格式化签名参数
		formatSignParams(file) {
			let formData = new FormData();
			formData.append('file', file);
			formData.append('type', '1');
			formData.append('showType', '1');
			return formData;
		},

		//签字
		async signUploadRequest(file) {
			let params = this.formatSignParams(file);
			let result = await attachUpload(params).then((res) => res.data);
			if (result.code === '0') {
				this.$emit('finish', this.$detailData(result.data));
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.sign_board_wrapper {
	h3 {
		font-size: 16px;
	}

	.time {
		text-align: right;
	}
}
</style>
